package com.zygame.sdk.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.ConfigMessage;
import com.zygame.sdk.bean.UserBean;
import com.zygame.sdk.db.UserDao;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.LoginUtil;
import com.zygame.sdk.utils.VerifyUtil;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.utils.toast.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Yuxl on 2017/9/28.
 *
 *
 */

public class Zy_Dialog_main extends ZY_BaseDialog {
    private Context context;
    private View view;
    private String mAccoutn;
    private String mPwd;
    private boolean isBack;
    private String randomPassword;
    private String user_name;
    private IWXAPI api;
    private static  Zy_Dialog_main dialog_main;
    public static Zy_Dialog_main getInstance(Context context){
        if(null == dialog_main){
            dialog_main = new Zy_Dialog_main(context);
        }
        return dialog_main;
    }


    private Zy_Dialog_main(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(context, ConstantValue.APP_ID,true);
        api.registerApp(ConstantValue.APP_ID);

    }

    @Override
    protected void init() {
        showClose(true);
        Button btn_register = (Button) findViewById(ResourceUtil.getId(context, "btn_register"));
        Button btn_login = (Button) findViewById(ResourceUtil.getId(context, "btn_login"));
        Button btn_qr = (Button) findViewById(ResourceUtil.getId(context, "btn_qr"));
        ImageView iv_google = (ImageView) findViewById(ResourceUtil.getId(context, "iv_google"));
        ImageView iv_wechat = (ImageView) findViewById(ResourceUtil.getId(context,"iv_wechat"));
        LinearLayout ll_more = (LinearLayout) findViewById(ResourceUtil.getId(context,"ll_more"));

        if(ConstantValue.ZY_SHOWBACK){
            btn_back.setVisibility(View.VISIBLE);
        }
        btn_qr.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        iv_google.setOnClickListener(this);
        iv_wechat.setOnClickListener(this);

        if(null !=ConfigMessage.getInstance().getPayListData() &&ConfigMessage.getInstance().getPayListData().size()>0){
            ll_more.setVisibility(View.VISIBLE);
            if(ConfigMessage.getInstance().getPayListData().contains("Google")){
                iv_google.setVisibility(View.VISIBLE);
            }
            if(ConfigMessage.getInstance().getPayListData().contains("Wechat")){
                iv_wechat.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    protected String getResourceStr() {
        return "zy_dialog_main";
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        int ids = v.getId();
        if (ResourceUtil.getId(context, "btn_register") == ids) {//注册
            new ZY_Dialog_Register(context).show();
            dismiss();
        }else if (ResourceUtil.getId(context, "btn_login") == ids) {//登录
            UserDao ud = new UserDao();
            UserBean ub = ud.query(ud._LAST_LOGIN_FLAG, "1");

//            if (ub != null) {
//                mAccoutn = ub.userName;
//                mPwd = ub.pwd;
////                HttpUtil.login(mContext, ub.userName, ub.pwd, onLoginCallback);
//                // new AccountCenter(getContext()).show();
//                HttpUtil.login(mAccoutn, mPwd,"1",mContext, onLoginCallback);
//                new ZY_Dialog_AccountCenter(context).show();
//                this.cancel();
//
//            } else {
                ZY_Dialog_Login login = new ZY_Dialog_Login(getContext());
                login.show();
//            Register register = new Register(getContext());
//            register.show();
//            }
            dismiss();
        }
        else if (ResourceUtil.getId(context, "btn_qr") == ids) {

            showLoad(true);
            HttpUtil.getName(mContext, onGetGameCallBac);
        }else if(ids == ResourceUtil.getId(context,"btn_back")){
            new ZY_Dialog_AccountCenter(context).show();
            dismiss();

        } else if(ResourceUtil.getId(context,"iv_google") == ids){
            dismiss();
            ZyGameSDK.getInstance().googleSignIn();
        }else if(ResourceUtil.getId(context,"iv_wechat") == ids){
            final SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_demo_test";
                api.sendReq(req);
//            dismiss();
        }

    }

    NetHttpUtil.DataCallback<JSONObject> onGetGameCallBac = new NetHttpUtil.DataCallback<JSONObject>() {
        @Override
        public void callbackSuccess(JSONObject paramObject) {
            LogHelper.i("获取名字接口", paramObject.toString());

            showLoad(false);
            String code = paramObject.optString("code").trim();
            if ("0".equals(code)) {
                dismiss();
                randomPassword = VerifyUtil.getRandomPassword(6);
                user_name = paramObject.optString("user_name");
                HttpUtil.reg(mContext, onRegCallback, user_name, randomPassword, "2");
                LogHelper.i("一键注册","use:"+paramObject.optString("user_name")+"pwd:"+ randomPassword);
            }else {
                ToastUtil.showToast(context, paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {

        }
    };


    NetHttpUtil.DataCallback<JSONObject> onRegCallback = new NetHttpUtil.DataCallback<JSONObject>() {
        @Override
        public void callbackSuccess(JSONObject paramObject) {

            LogHelper.i("注册接口", paramObject.toString());
            String code = paramObject.optString("code").trim();
            if ("0".equals(code)) {
                LoginUtil.loginResult(user_name, randomPassword, paramObject,mContext,"1",1,"");
                Map<String, String> map = new HashMap<String, String>();
                map.put("mUserName", user_name);
                map.put("mPassword", randomPassword);
                new SaveAccountView(mContext, map);
                ZyGameSDK.getInstance().onLoginResult(UserModel.getInstance());
                Toast.makeText(context,ResourceUtil.getStringId(context,"GSDK_savealbum_text"),Toast.LENGTH_SHORT).show();
            }else {
                ToastUtil.showToast(context, paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {
            ToastUtil.showToast(context, error);
        }
    };


    NetHttpUtil.DataCallback<JSONObject> onLoginCallback = new NetHttpUtil.DataCallback<JSONObject>() {

        @Override
        public void callbackSuccess(JSONObject paramObject) {
            LogHelper.i("login接口",paramObject.toString());

//            LoginUtil.loginResult(mAccoutn,mPwd,paramObject,context);
            String code = paramObject.optString("code");

        }

        @Override
        public void callbackError(String error) {
//            ZyGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_FAIL, "65 sdk login fail !");
        }
    };


    public void dissDialog(){
        dialog_main.dismiss();
        dialog_main = null;
    }
}
